Skip to content

Method: containsWithInference(Resource, Property, RDFNode, Collection)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.jena.connector;
14:
15: import org.apache.jena.rdf.model.Property;
16: import org.apache.jena.rdf.model.RDFNode;
17: import org.apache.jena.rdf.model.Resource;
18: import org.apache.jena.rdf.model.Statement;
19:
20: import java.util.Collection;
21: import java.util.List;
22: import java.util.Map;
23: import java.util.stream.Collectors;
24:
25: /**
26: * This connector implementation supports proper inference.
27: */
28: class SnapshotStorageConnectorWithInference extends SnapshotStorageConnector implements InferredStorageConnector {
29:
30: private final Map<String, String> reasonerConfig;
31:
32: SnapshotStorageConnectorWithInference(AbstractStorageConnector centralConnector,
33: Map<String, String> reasonerConfig) {
34: super(centralConnector);
35: this.reasonerConfig = reasonerConfig;
36: }
37:
38: @Override
39: void snapshotCentralDataset() {
40: final SnapshotStorageWithInference s = new SnapshotStorageWithInference(configuration, reasonerConfig);
41: s.addCentralData(centralConnector.getStorage().getDataset());
42: this.storage = s;
43: }
44:
45: @Override
46: public List<Statement> find(Resource subject, Property property, RDFNode value, Collection<String> contexts) {
47: ensureTransactionalState();
48: if (contexts.isEmpty()) {
49: return ((SnapshotStorageWithInference) storage).getRawDefaultGraph()
50: .listStatements(subject, property, value).toList();
51: } else {
52: return contexts.stream().map(ctx -> ((SnapshotStorageWithInference) storage).getRawNamedGraph(ctx)
53: .listStatements(subject,
54: property, value)
55: .toList())
56: .flatMap(Collection::stream).collect(Collectors.toList());
57: }
58: }
59:
60: @Override
61: public boolean contains(Resource subject, Property property, RDFNode value, Collection<String> contexts) {
62: ensureTransactionalState();
63: if (contexts.isEmpty()) {
64: return ((SnapshotStorageWithInference) storage).getRawDefaultGraph().contains(subject, property, value);
65: } else {
66: return contexts.stream().anyMatch(c -> ((SnapshotStorageWithInference) storage).getRawNamedGraph(c)
67: .contains(subject, property,
68: value));
69: }
70: }
71:
72: @Override
73: public List<Statement> findWithInference(Resource subject, Property property, RDFNode value,
74: Collection<String> contexts) {
75: ensureTransactionalState();
76: if (contexts.isEmpty()) {
77: return storage.getDefaultGraph().listStatements(subject, property, value).toList();
78: } else {
79: return contexts.stream()
80: .map(ctx -> storage.getNamedGraph(ctx).listStatements(subject, property, value).toList())
81: .flatMap(Collection::stream).collect(Collectors.toList());
82: }
83: }
84:
85: @Override
86: public boolean containsWithInference(Resource subject, Property property, RDFNode value,
87: Collection<String> contexts) {
88: ensureTransactionalState();
89:• if (contexts.isEmpty()) {
90: return storage.getDefaultGraph().contains(subject, property, value);
91: } else {
92: return contexts.stream().anyMatch(c -> storage.getNamedGraph(c).contains(subject, property, value));
93: }
94: }
95:
96: @Override
97: public boolean isConsistent(String context) {
98: ensureTransactionalState();
99: return ((SnapshotStorageWithInference) storage).checkConsistency(context).isValid();
100: }
101: }